<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/3/4
 * Time: 下午7:46
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
use Lib\MyTree;

class RegionController extends AdminBaseController
{
    /**
     * 地区表
     */
    public function index()
    {
        $region_data = M('region')->order('pid asc')->select();
        $region_tree = new MyTree();
        $region_info = $region_tree->my_tree($region_data);
        $this->assign('region_info',$region_info);
        $this->assign('title','TD商城地区列表');
        $this->display('index');
    }

    /**
     * 添加地区
     */
    public function add()
    {
        if(IS_GET){
            $region_data = M('Region')->order('pid asc')->select();
            $my_tree = new MyTree();
            $region_info = $my_tree->my_tree($region_data);
            $this->assign('region_info',$region_info);
            $this->assign('title','FD商城地区添加');
            $this->display();
            exit;
        }
        if(IS_AJAX){
            //获取地区名
            $region_name = I('post.region_name','');
            if(empty($region_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'地区名不能为空！']);
            }
            //获取父级id
            $pid = I('post.pid',0);
            if(M('region')->where(array('region_name'=>$region_name,'pid'=>$pid))->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'此地区已经存在！']);
            }
            if(M('region')->add(['pid'=>$pid,'region_name'=>$region_name])){
                $this->ajaxReturn(['state'=>0,'msg'=>'添加地区成功！']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加地区失败！']);
        }
    }

    /**
     * 修改权限
     */
    public function edit()
    {
        if(IS_GET){
            $id = I('get.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $region_info = M('region')->where(['id'=>$id])->find();
            $region_data = M('region')->order('pid asc')->select();
            $my_tree = new MyTree();
            $all_region = $my_tree->my_tree($region_data);
            $this->assign('all_region',$all_region);
            $this->assign('region_info',$region_info);
            $this->assign('title','FD商城地区修改');
            $this->display();
        }
        if(IS_POST){
            //获取地区id
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //获取地区名
            $region_name = I('post.region_name','');
            if(empty($region_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'地区名不能为空！']);
            }
            //获取地区父级ID
            $pid = I('post.pid',0);
            $region = M('region');
            $this_region_info = $region->field('pid,region_name')->where(['id'=>$id])->find();
            //如果查不到就报错
            if(empty($this_region_info)){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能修改不存在的地区！']);
            }
            //如果没有修改直接报成功
            if($this_region_info['pid'] == $pid && $this_region_info['region_name'] == $region_name){
                $this->ajaxReturn(['state'=>0,'msg'=>'修改成功！']);
            }
            //如果修改过后的地区在数据库中已经存在则报错
            if($region->where(['pid'=>$pid,'region_name'=>$region_name])->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能修改为已经存在的地区！']);
            }
            if($region->where(['id'=>$id])->setField(['pid'=>$pid,'region_name'=>$region_name]) === false){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'修改成功！']);
        }
    }

    /**
     * 删除地区
     */
    public function del(){
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            if(M('region')->where(['id'=>$id])->getField('id') == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除不存在的地区！']);
            }
            if(M('region')->where(['pid'=>$id])->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除有孩子的地区！']);
            }
            if(M('region')->delete($id) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }
}